package org.adorsys.aderp.orga.web;

import java.util.Collections;

import org.adorsys.aderp.orga.domain.OrgaResource;
import org.adorsys.aderp.orga.domain.ResResRelation;
import org.adorsys.aderp.orga.domain.ResourceRelationRole;
import org.adorsys.aderp.orga.service.OrgaResourceService;
import org.adorsys.aderp.orga.service.ResResRelationService;
import org.adorsys.aderp.orga.service.ResourceRelationRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping("/resresrelations")
@Controller
@RooWebScaffold(path = "resresrelations", formBackingObject = ResResRelation.class)
public class ResResRelationController {
	
 
	 @RequestMapping(params = "form", produces = "text/html")
	    public String createForm(@RequestParam(value="resourceId", required=false)Long resourceId, 
	    		@RequestParam(value="targetResourceId", required=false)Long targetResourceId,
	    		@RequestParam(value="relationRoleId", required=false)Long relationRoleId,
	    		Model uiModel) {
	    	populateCreateForm(uiModel, new ResResRelation(), resourceId,targetResourceId,relationRoleId);
	        return "resresrelations/create";
	    }
	    

	    @RequestMapping(produces = "text/html")
	    public String list(@RequestParam(value="resourceId", required=false)Long resourceId, 
	    		@RequestParam(value="targetResourceId", required=false)Long targetResourceId,
	    		@RequestParam(value="relationRoleId", required=false)Long relationRoleId,
	    		@RequestParam(value = "page", required = false, defaultValue="0") Integer page, 
	    		@RequestParam(value = "size", required = false, defaultValue="10") Integer size, Model uiModel) {
	    	
			OrgaResource orgaResource = resourceId!=null? orgaResourceService.findOrgaResource(resourceId):null;
			OrgaResource targetResource = targetResourceId!=null? orgaResourceService.findOrgaResource(targetResourceId):null;
			ResourceRelationRole relationRole = relationRoleId!=null? resourceRelationRoleService.findResourceRelationRole(relationRoleId):null;

	    	Page<ResResRelation> ResourcePage = resResRelationService.searchResResRelation(orgaResource, targetResource, relationRole, page, size);
	        uiModel.addAttribute("resresrelations", ResourcePage.getContent());
	        uiModel.addAttribute("maxPages", ResourcePage.getTotalPages());

	        addDateTimeFormatPatterns(uiModel);

	        return "resresrelations/list";
	    }    
	    
	    void populateCreateForm(Model uiModel, ResResRelation resResRelation, 
	    		Long resourceId,Long targetResourceId,Long relationRoleId) {
	    	OrgaResource orgaResource = resourceId!=null? orgaResourceService.findOrgaResource(resourceId):null;
			OrgaResource targetResource = targetResourceId!=null? orgaResourceService.findOrgaResource(targetResourceId):null;
			ResourceRelationRole relationRole = relationRoleId!=null? resourceRelationRoleService.findResourceRelationRole(relationRoleId):null;

			resResRelation.setOrgaResource(orgaResource);
			resResRelation.setTargetResource(targetResource);
			resResRelation.setRelationRole(relationRole);

			uiModel.addAttribute("resResRelation", resResRelation);
	        addDateTimeFormatPatterns(uiModel);
	        
	        if(orgaResource!=null){
	        	uiModel.addAttribute("orgaresources", Collections.singleton(orgaResource));        	
	        } else {
	        	uiModel.addAttribute("orgaresources", orgaResourceService.findAllOrgaResources());        	
	        }
	        
	        if(targetResource!=null){
	        	uiModel.addAttribute("targetresources", Collections.singleton(targetResource));        	
	        } else {
	        	uiModel.addAttribute("targetresources", orgaResourceService.findAllOrgaResources());        	
	        }

	        if(relationRole!=null){
	        	uiModel.addAttribute("resourcerelationroles", Collections.singleton(relationRole));        	
	        } else {
	            uiModel.addAttribute("resourcerelationroles", resourceRelationRoleService.findAllResourceRelationRoles());
	        }
	    }
}
